package zh;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;

import robocode.*;
import zh.logging.MyLogger;
/**
 *
 * @author zheismann
 */
public class Enemy
{
    private static MyLogger logger = MyLogger.getLogger(Enemy.class.getName());
    static {logger.setLevel(Level.FINER);}
    
    private long lastEventReceivedTime;
    private final String name;
    private ScannedRobotEvent data;
    private static final int HISTORY_SIZE = 5;
    private int numTimesUpdated = 0;
    private Point2D[] positions = new Point2D[HISTORY_SIZE];
    private double[] distances = new double[HISTORY_SIZE];
    private static Point2D ZERO = new Point2D.Double(0.0, 0.0);
    
    private double numBulletsFiredAtMe;
    private double numBulletsHitMe;
    
    /** Creates a new instance of Enemy */
    public Enemy(ScannedRobotEvent event, Point2D enemyLocation)
    {
        Arrays.fill(positions, ZERO);
        name = event.getName();
        updateData(event, enemyLocation);
    }
    private int LIMIT = 0;
    public void updateData(ScannedRobotEvent event, Point2D enemyLocation)
    {
        if (event.getTime() < lastEventReceivedTime)
        {
            logger.log(Level.WARNING, "Old data from an event");
            return;
        }
        numTimesUpdated++;
        data = event;
        lastEventReceivedTime = event.getTime();

        Point2D tmp = null;
        double dbl = 0.0;
        for (int i = HISTORY_SIZE - 1; i > 0; i--)
        {
            tmp = positions[i - 1];
            positions[i] = tmp;
            dbl = distances[i - 1];
            distances[i] = dbl;
        }
        positions[0] = enemyLocation;
        distances[0] = data.getDistance();
    }
    
    public double getSlope()
    {
        double slope = 0.0;
        Point2D a = positions[0];
        Point2D b = positions[1];
        double yDiff = a.getY() - b.getY();
        double xDiff = a.getX() - b.getX();
        if (yDiff != 0.0)
        {
            slope = yDiff / xDiff;
        }
        
        double yIntercept = b.getY();
        // y = mx + b;
        // yIntercept  = slope * b.getX() + yIntercept
        
        return slope;
    }
    
    public int getNumTimesUpdated()
    {
        return numTimesUpdated;
    }
    
    public String getName()
    {
        return name;
    }
    
    public double getBearing()
    {
        return data.getBearing();
    }
    
    public boolean equals(Object o)
    {
        if (o == null || !(o instanceof Enemy))
        {
            return false;
        }
        return name.equals(((Enemy)o).getName());
    }
    
    public int hashCode()
    {
        return name.hashCode();
    }
    
    public double getDistance()
    {
        return data.getDistance();
    }
    
    public double getLastUpdatedTime()
    {
        return data.getTime();
    }
    
    public double getEnergy()
    {
        return data.getEnergy();
    }
    public Point2D getLocation()
    {
        return positions[0];
    }
    
    public void addBulletFiredAtMe()
    {
        numBulletsFiredAtMe+=1.0;
    }
    
    public void bulletHitMe()
    {
//        System.out.println("bulletHitMe");
        numBulletsHitMe+=1.0;
    }
    
    public double getHitPercentage()
    {
        double hitPercentage = numBulletsHitMe/(numBulletsFiredAtMe!=0.0?numBulletsFiredAtMe:1.0);
        logger.log(Level.FINE, "hitPercentage: " + hitPercentage + " " + numBulletsHitMe + "/("+(numBulletsFiredAtMe!=0.0?numBulletsFiredAtMe:1.0)+")");
        return hitPercentage;
    }
}
